# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

library(igraph)

g <- read.graph(file = "fa-core-graphml.xml", format = "graphml")

### TOT ###

# Degree distribution
dtot <- degree(g, mode = "total")
# start fitting for x >= 20
# alphatot = 3.024414
alphatot <- power.law.fit(dtot, xmin = 20)
ddtot <- degree.distribution(g, mode = "total")

pdf("fa-core-tot-degree-distribution.pdf")
plot(ddtot, log = "xy", xlab = "Degree", ylab = "Frequency", col = 1, main = "FA Core Degree Distribution")
lines(28:150, 1000*(28:150)^(-coef(alphatot)), col = 2)
dev.off()


# Degree cumulative distribution
ddtot <- degree.distribution(g, mode = "total", cumulative = TRUE)

pdf("fa-core-tot-cumulative-degree-distribution.pdf")
plot(ddtot, log="xy", xlab = "Degree", ylab = "Cumulative frequency", col = 1, main = "FA Core Cumulative Degree Distribution")
lines(30:200, 10^4*(30:200)^(-coef(alphatot)), col = 2)
dev.off()

### IN ###

# In-degree distribution
ddin <- degree.distribution(g, mode = "in")
din <- degree(g, mode = "in")
# alphain: 1.931511
alphain <- power.law.fit(din, xmin = 5)

pdf("fa-core-in-degree-distribution.pdf")
plot(ddin, log = "xy", xlab = "Degree", ylab = "Frequency", col = 1, main = "FA Core In-Degree Distribution")
lines(6:100, 4*(6:100)^(-coef(alphain)), col = 2)
dev.off()

# In-degree cumulative distribution

ddin <- degree.distribution(g, mode = "in", cumulative = TRUE)

pdf("fa-core-in-cumulative-degree-distribution.pdf")
plot(ddin, log="xy", xlab = "Degree", ylab = "Cumulative frequency", col = 1, main = "FA Core Cumulative In-Degree Distribution")
dev.off()

### OUT ###

# Out-degree distribution
ddout <- degree.distribution(g, mode = "out")
xs <- seq(0, 40, 1)

pdf("fa-core-out-degree-distribution.pdf")
plot(ddout, xlab = "Degree", ylab = "Frequency", col = 1, main = "FA Core Out-Degree Distribution")
points(xs, dpois(xs, lambda = 14), col = 2, pch = 2)
dev.off()

# Out-degree cumulative distribution
dout <- degree(g, mode = "out")
ddout <- degree.distribution(g, mode = "out", cumulative = TRUE)
# alphaout = 10.46381
alphaout <- power.law.fit(dout, xmin = 20)

pdf("fa-core-out-cumulative-degree-distribution.pdf")
plot(ddout, log="xy", xlab = "Degree", ylab = "Cumulative frequency", col = 1, main = "FA Core Cumulative Out-Degree Distribution")
lines(15:28, 10^12*(15:28)^(-coef(alphaout)), col = 2)
dev.off()


